import numpy as np


class StandardScale:
    """
    均值方差归一化
    """

    def __init__(self):
        self.mean_ = None
        self.std_ = None

    def fit(self, X):
        self.mean_ = np.array([np.mean(X[:, col]) for col in range(X.shape[1])])
        self.std_ = np.array([np.std(X[:, col]) for col in range(X.shape[1])])
        return self

    def transform(self, X):
        assert X.shape[1] == self.std_.shape[0]
        rtX = np.empty(shape=X.shape, dtype=float)
        for index in range(X.shape[1]):
            rtX[:, index] = (X[:, index] - self.mean_[index]) / self.std_[index]
        return rtX

